package com.yc.cloud.openai.portal.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.oss.service.OssFileService;
import com.yc.cloud.openai.entity.AudioRecord;
import com.yc.cloud.openai.mapper.AudioRecordMapper;
import com.yc.cloud.openai.portal.dto.request.AudioRecordListParam;
import com.yc.cloud.openai.portal.service.AudioRecordService;
import com.yc.cloud.openai.vo.AudioRecordVo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.val;

/**
 * <p>
 * AI音频记录 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2025-02-19
 */
@Service
public class AudioRecordServiceImpl extends ServiceImpl<AudioRecordMapper, AudioRecord> implements AudioRecordService {

    private final OssFileService ossFileService;

    public AudioRecordServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<AudioRecordVo> list(AudioRecordListParam param, BaseQueryParam queryParam) {
        val query = new QueryWrapper<AudioRecord>();
        IPage<AudioRecord> page;
        val clientIds = StrUtil.split(param.getClientId(), ",");
        if (clientIds.size() == 1) {
            query.eq(AudioRecord.CLIENT_ID, clientIds.get(0));
        } else {
            query.in(AudioRecord.CLIENT_ID, clientIds);
        }
        // 增加任务状态过滤条件
        if (CollUtil.isNotEmpty(param.getStatusList())) {
            query.in(AudioRecord.STATUS, param.getStatusList());
        }
        // 排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        // 模糊查询
        // if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
        //     query.and(queryWrapper -> {

        //     });
        // }
        // 分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val pageList = baseMapper.selectPage(page, query);
        return pageList.convert(this::convertVo);
    }

    @Override
    public AudioRecord create(AudioRecord param) {
        val audiorecord = new AudioRecord();
        BeanUtils.copyProperties(param, audiorecord);
        val saveFlag = save(audiorecord);
        return saveFlag ? audiorecord : null;
    }

    @Override
    public AudioRecordVo get(Long id) {
        return convertVo(getById(id));
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public AudioRecord update(Long id, AudioRecord param) {
        val audiorecord = new AudioRecord();
        BeanUtils.copyProperties(param, audiorecord);
        audiorecord.setId(id);
        val updateFlag = updateById(audiorecord);
        return updateFlag ? audiorecord : null;
    }

    @Override
    public AudioRecordVo convertVo(AudioRecord audiorecord) {
        if (audiorecord == null) {
            return null;
        }
        val vo = new AudioRecordVo();
        BeanUtils.copyProperties(audiorecord, vo);
        if (StrUtil.isNotEmpty(audiorecord.getFileId())) {
            val ossFile = ossFileService.getByFileId(audiorecord.getFileId());
            if (ossFile != null) {
                vo.setOssFileVo(ossFile);
            }
        }
        return vo;
    }

    @Override
    public List<AudioRecordVo> convertVo(List<AudioRecord> AudioRecordList) {
        if (CollUtil.isNotEmpty(AudioRecordList)) {
            return AudioRecordList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
